package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.BlogMapper;
import cn.tedu.weibo.mapper.UserMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Blog;
import cn.tedu.weibo.pojo.vo.BlogIndexVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.result.JsonResult;
import cn.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

import cn.tedu.weibo.pojo.entity.User;
import springfox.documentation.annotations.ApiIgnore;

//处理和微博相关的操作
@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "博客模块")
@Validated
public class BlogController {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/insert")
    @ApiOperation("发表微博")
    public JsonResult addBlog(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        //首先验证是否登录
        User user = (User)session.getAttribute("user");
        if(user==null){
            //没有登录
            return new JsonResult(Status.NOT_LOGIN);
        }
        Blog blog = new Blog();
        blog.setContent(weiboDTO.getContent());
        blog.setCreated(new Date());
        blog.setUserId(user.getId());
        blogMapper.insert(blog);
        return JsonResult.success();
    }

    @GetMapping("/selectIndex")
    @ApiOperation("首页微博列表")
    public JsonResult findIndexBlog(){
        /*
            1.首先从redis中获取"weiboList"对应的List集合
            2.返回值为NULL(还没有放入过redis)
                2.1:通过Mapper从数据库中查询数据
                2.2:将查询的集合存入到redis中
                2.3:将该集合返回给客户端
            3.返回值不为NULL
                3.1:直接将该集合返回给客户端
         */
        ValueOperations ops = redisTemplate.opsForValue();
        List<BlogIndexVO> list = (List<BlogIndexVO>) ops.get("weiboList");
        if(list==null){
            list = blogMapper.findAll();//去数据库中查数据
            ops.set("weiboList",list);//存入redis
        }
        return JsonResult.success(list);
    }
    @GetMapping("/selectById")
    @ApiOperation("查看微博详情")
    /**
     * 当我们方法的参数不是一个DTO,而是一个值那么可以使用@ApiImplicitParam在文档网页上说明该参数
     */

    @ApiImplicitParam(name="id",value = "博客ID",required = true,example = "1",dataType = "int")
    public JsonResult findById(@Range(min = 1,max = 9999,message = "微博ID只能在1~9999之间") @RequestParam Integer id){
        /*if(id<1){
            throw new IllegalArgumentException("ID不能<1");
        }*/
        return JsonResult.success(blogMapper.findById(id));
    }
    @GetMapping("/findByIdAndNickname")
    @ApiOperation("根据博客ID和昵称查询微博详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "博客ID",required = true,example = "1",dataType = "int"),
            @ApiImplicitParam(name = "nickname",value = "作者昵称",required = true,example = "阿三",dataType = "String")
    })
    public JsonResult findByIdAndNickname(Integer id,String nickname){
        //不具体实现了,主要介绍方法上的文档注解
        return JsonResult.success();
    }

}
